package com.llkj.qingnianguoxue.home;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.llkj.qingnianguoxue.R;
import com.llkj.qingnianguoxue.adapter.HotRankingAdapter;
import com.llkj.qingnianguoxue.base.BaseFragment;
import com.llkj.qingnianguoxue.bean.HotRankingBean;
import com.llkj.qingnianguoxue.http.HttpMethod;
import com.llkj.qingnianguoxue.util.DisplayUtil;
import com.llkj.qingnianguoxue.util.GsonUtil;
import com.llkj.qingnianguoxue.util.helper.LoadDataHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * 大师详情——视频
 */
public class MasterVideoFragment extends BaseFragment implements BaseQuickAdapter.RequestLoadMoreListener,
        LoadDataHelper.LoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
    private SwipeRefreshLayout refreshLayout;
    private RecyclerView lvActions;
    private HotRankingAdapter rankingAdapter;
    private List<HotRankingBean.RankingBean> mRankData;
    private int totalSize, mPage;
    private String mMasterId;//大师id

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_master_video, container, false);
        Bundle bundle = getArguments();
        mMasterId = bundle.getString("master_id");

        initView();
        initListener();

        onRefresh();
        return rootView;
    }

    private void initView() {
        refreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.refresh_layout);
        refreshLayout.setColorSchemeResources(R.color.blue3989ff);
        lvActions = (RecyclerView) rootView.findViewById(R.id.recyclerView);
        int padding = DisplayUtil.dip2px(getActivity(), 10);
        lvActions.setPadding(padding, 0, padding, padding);
        lvActions.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));

        mRankData = new ArrayList<>();
        rankingAdapter = new HotRankingAdapter(R.layout.item_hot_ranking, mRankData, null);
        rankingAdapter.setType(4);
        lvActions.setAdapter(rankingAdapter);
        lvActions.setNestedScrollingEnabled(false);
    }

    private void initListener() {
        refreshLayout.setOnRefreshListener(this);
        rankingAdapter.setOnLoadMoreListener(this);
        lvActions.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                HotRankingBean.RankingBean item = mRankData.get(i);
                Bundle bundle = new Bundle();
                bundle.putString("id", item.getId());
                startActivity(VideoDetailActivity.class, bundle);
            }
        });
    }

    @Override
    public void onLoadMore() {
        mPage++;
        getVideo();
    }

    @Override
    public void onRefresh() {
        mPage = 1;
        getVideo();
    }

    @Override
    public void onLoadMoreRequested() {
        LoadDataHelper.loadMoreRequest(this, rankingAdapter, null, lvActions, totalSize);
    }

    private void getVideo() {
        HttpMethod.artistChoiceness(getActivity(), this, mMasterId, String.valueOf(mPage));
    }

    @Override
    protected void onhttpSuccess(String data, int method) {
        super.onhttpSuccess(data, method);
        if (method == HttpMethod.HTTP_ARTIST_CHOICENESS) {
            HotRankingBean hotRankingBean = GsonUtil.GsonToBean(data, HotRankingBean.class);
            if (hotRankingBean != null) {
                totalSize = Integer.parseInt(hotRankingBean.getNum());
                if (mPage == 1) {
                    mRankData.clear();
                    LoadDataHelper.refreshComplete(rankingAdapter, mRankData, refreshLayout, lvActions);
                } else {
                    rankingAdapter.setEnableLoadMore(true);
                    rankingAdapter.loadMoreComplete();
                }
                mRankData.addAll(hotRankingBean.getList());
                rankingAdapter.notifyDataSetChanged();
            }
        }
    }
}
